# from django.shortcuts import render
# from django.core import serializers
# from django.shortcuts import HttpResponse
# import json,time
# from db.mydb import MyDBConnection
# from db.utils.dfutils import load_json_to_object
# from db.utils.logutils import LogUtils
# from db.dao.student import Student
# from db.dao.course import Course
# from db.dao.enrollment import Enrollment
#
# def enrollment_index(request):
#     current = 1#如果用户不提供，默认看第一页
#     pagesize = 5#如果用户不提供，默认每页5条
#     if request.method == 'GET':  #大小写
#         myDBConnection = MyDBConnection()
#         current = request.GET.get("current")
#         pagesize = request.GET.get("pagesize")
#         print(f'1111 {request.GET.get("current")}')
#         offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
#         enrollments_json = myDBConnection.fetch_data_by_sql(f"select * from enrollment limit {offset},{pagesize}")
#         msg = f'现在查看的是第{current}页，每页大小{pagesize}'
#         print(enrollments_json)
#         code = 200
#         rep_json =  {"code":code,"msg":msg,"enrollments":enrollments_json}
#     if bool(request.GET.get('json'))==True:
#         return HttpResponse(json.dumps(rep_json), content_type="application/json")
#     else:
#         return render(request, "enrollment/index.html", rep_json)
# def enrollment_Bycid(request):
#     current = 1#如果用户不提供，默认看第一页
#     pagesize = 5#如果用户不提供，默认每页5条
#     if request.method == 'GET':  #大小写
#         myDBConnection = MyDBConnection()
#         current = request.GET.get("current")
#         pagesize = request.GET.get("pagesize")
#         c_id = request.GET.get("c_id")
#
#         offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
#         enrollments_json = myDBConnection.fetch_data_by_sql(f"select * from enrollment where c_id={c_id} order by e_enrollmentDate limit {offset},{pagesize} ")
#         session = myDBConnection.get_session()
#         enrollments=[]
#         for enrollment in enrollments_json:
#             student = session.query(Student).filter(Student.s_id == enrollment["s_id"]).first()
#             print("----------------")
#             print(type(student))
#             enrollment["_student"]=student.to_json()
#             enrollments.append(enrollment)
#         session.flush()
#         session.close()
#         msg = f'现在查看的是第{current}页，每页大小{pagesize}'
#         print(enrollments)
#         code = 200
#         rep_json =  {"code":code,"msg":msg,"enrollments":enrollments}
#     if bool(request.GET.get('json'))==True:
#         return HttpResponse(json.dumps(rep_json,ensure_ascii=False), content_type="application/json")
#     else:
#         return render(request, "enrollment/index.html", rep_json)
#
# def enrollment_add(request):
#     # p_id = request.POST.get("p_id")
#     # p_name = request.POST.get("p_name")
#     data = request.body.decode("utf-8") #获得提交的json格式的字符串参数
#     myDBConnection =MyDBConnection()  #连接数据库
#     enrollment_json_obj = json.loads(data)  #j字符串转换为json
#     enrollment = load_json_to_object(enrollment_json_obj,Enrollment()) #json对象转换为指定对象Enrollment
#     myDBConnection.insert_entity(enrollment) #新增一条gameevents，如果不需要存到mysql只使用大数据日志系统，注释本句
#     #----------关联查询开始-------------
#     session = myDBConnection.get_session()
#     course = session.query(Course).filter(Course.c_id == enrollment.c_id).first()
#     student = session.query(Student).filter(Student.s_id==enrollment.s_id).first()
#     session.flush()
#     session.close()
#     #----------关联查询结束-------------
#     enrollment._student = student
#     enrollment._course= course
#     #------------日志记录开始 ，使用自定义日志记录器LogUtils.log----------
#     path_name = '/usr/ftp/logs/enrollment/'
#     current_time = time.localtime(time.time())
#
#     current_time_str = time.strftime('%Y-%m-%d', current_time)  # 参数2为格式化字符串
#     print(current_time_str)
#     file_name = current_time_str + '_enrollment.log'
#     LogUtils.log(path_name,file_name,enrollment.format_str())
#
#     msg = f"{enrollment.e_id}选课事件添加成功"
#     code = 200
#     rep_json = {"code":code,"msg":msg}
#     return HttpResponse(json.dumps(rep_json,ensure_ascii=False), content_type="application/json")